﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.Metadata.Entity.DataModel
{
    /// <summary>
    /// 实体对象属性
    /// </summary>
    [Serializable]
    public class EntityObjectAttribute : ICloneable, IRefSchemaDetail
    {
        /// <summary>
        /// 主键id
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// EO外键id
        /// </summary>
        [JsonIgnore]
        public string EntityObjectID { get; set; }

        /// <summary>
        /// 属性标识
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 属性名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 属性类型
        /// </summary>
        public string DataType { get; set; }

        /// <summary>
        /// 长度
        /// </summary>
        public int Length { get; set; }

        /// <summary>
        /// 精度
        /// </summary>
        public int Precision { get; set; }

        /// <summary>
        /// 当本Attr数据类型为EntityObject时，本字段保存关联EO id
        /// </summary>
        public string RelatedEntityObjectID { get; set; }

        /// <summary>
        /// 是否集合
        /// </summary>
        public bool IsList { get; set; }

        /// <summary>
        /// 是否可空
        /// </summary>
        public bool IsNullable { get; set; }

        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue { get; set; }

        /// <summary>
        /// 默认值表达式json
        /// </summary>
        public string DefaultValueJson { get; set; }

        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext1 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext2 { get; set; }

        /// <summary>
        /// 备用字段
        /// </summary>
        [JsonIgnore]
        public string Ext3 { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string Creator { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 最后修改人
        /// </summary>
        //[DataMember]
        [JsonIgnore]
        public string LastModifier { get; set; }

        /// <summary>
        /// 最后修改时间
        /// </summary>        
        //[DataMember]
        [JsonIgnore]
        public DateTime LastModifyTime { get; set; }

        public EntityObject SubEntity { get; set; }

        public List<EntityObject> SubEntitys { get; set; }

        public object Clone()
        {
            var newAttr =  this.MemberwiseClone() as EntityObjectAttribute;

            if (SubEntity != null)
            {
                newAttr.SubEntity = this.SubEntity.Clone() as EntityObject;
            }
            if (SubEntitys != null)
            {
                newAttr.SubEntitys = new List<EntityObject>();

                this.SubEntitys.ForEach(i => newAttr.SubEntitys.Add(i.Clone() as EntityObject));
            }

            return newAttr;
        }

        public string GetRefSchemaExpr(IMetaDataContent metaData)
        {
            if (metaData is EntityObject eo)
            {
                var top = eo.GetTopRefSchemaExpr();
                return $"{top}.{EntityObject.RefSchemaAttrAlias}.{this.Code}";
            }
            return null;
        }
    }
}
